package com.sinaapp.kltq.dinosaurweather;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinaapp.kltq.data.WeatherEnvironment;
import com.sinaapp.kltq.data.WeatherZhishu;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ZhishusFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ZhishusFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ZhishusFragment extends Fragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ZhishusFragment.
     */
    public static ZhishusFragment newInstance(String param1, String param2) {
        ZhishusFragment fragment = new ZhishusFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public ZhishusFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_zhishus, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onStart() {
        WeatherManager wm = new WeatherManager(getActivity().getApplicationContext());
        // 获取城市
        int cityIndex;
        int activityLayoutId;
        if (getActivity().getClass() == FlipWhiteActivity.class) {
            cityIndex = ((FlipWhiteActivity)getActivity()).cityIndex;
            activityLayoutId = R.id.whiteTotal;
        } else {
            cityIndex = ((FlipBlackActivity)getActivity()).cityIndex;
            activityLayoutId = R.id.blackTotal;
        }
        String city = wm.getCityList().get(cityIndex).city;
        // 获取数据
        WeatherEnvironment environment = wm.getEnvironmentWeather(city);
        List<WeatherZhishu> zhishuList = wm.getZhishuWeather(city);
        // 显示背景图
        RelativeLayout layout = ((RelativeLayout) getActivity().findViewById(activityLayoutId));
        if (layout != null) {
            layout.setBackgroundResource(R.drawable.zhishu_bk);
        }
        // 显示环境
        if (environment != null) {
            if (environment.quality == null) {   // 如果不存在环境数据
                ((TextView) getActivity().findViewById(R.id.envirQuality)).setText(" ");
                ((TextView) getActivity().findViewById(R.id.aqiText)).setText(" ");
                ((TextView) getActivity().findViewById(R.id.aqiValue)).setText(" ");
                ((TextView) getActivity().findViewById(R.id.pm25Text)).setText(" ");
                ((TextView) getActivity().findViewById(R.id.pm25Value)).setText(" ");
            } else {
                ((TextView) getActivity().findViewById(R.id.envirQuality)).setText(environment.quality);
                ((TextView) getActivity().findViewById(R.id.aqiValue)).setText(String.format("%d", environment.aqi));
                ((TextView) getActivity().findViewById(R.id.pm25Value)).setText(String.format("%d", environment.pm25));
            }
        }
        // 显示指数
        if (zhishuList != null) {
            WeatherZhishu zhishu;
            ((TextView) getActivity().findViewById(R.id.zhishuChuanyi)).setText(getZhiShuValue("穿衣指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuFangshai)).setText(getZhiShuValue("紫外线强度", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuGanmao)).setText(getZhiShuValue("感冒指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuYusan)).setText(getZhiShuValue("雨伞指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuYundong)).setText(getZhiShuValue("运动指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuYouwan)).setText(getZhiShuValue("旅游指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuLiangshai)).setText(getZhiShuValue("晾晒指数", zhishuList));
            ((TextView) getActivity().findViewById(R.id.zhishuXiche)).setText(getZhiShuValue("洗车指数", zhishuList));
        }
        super.onStart();
    }

    private WeatherZhishu getZhishu(String name, List<WeatherZhishu> data) {
        for (WeatherZhishu zhishu : data) {
            if (zhishu.name.compareTo(name) == 0)
                return zhishu;
        }
        return null;
    }

    private String getZhiShuValue(String name, List<WeatherZhishu> data) {
        WeatherZhishu zhishu;
        if ((zhishu = getZhishu(name, data)) != null)
            return zhishu.value;
        else
            return "无数据";
    }

    private String getZhiShuDetail(String name, List<WeatherZhishu> data) {
        WeatherZhishu zhishu;
        if ((zhishu = getZhishu(name, data)) != null)
            return zhishu.detail;
        else
            return "无数据";
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }

}
